/*
 * Syntelos 'SX'
 * Copyright (C) 1998, 2007  John Pritchard.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 */
package syntelos.sx.methods.e;

import syntelos.sx.Headers;
import syntelos.sx.Response;
import syntelos.sx.Request;
import syntelos.sx.Server;
import syntelos.sx.Socket;

import alto.io.Output;
import alto.lang.Buffer;
import alto.lang.Type;
import alto.sys.Reference;


/**
 * Compile UEL source <code>"/e/file"</code> to target
 * <code>"/file"</code>.
 * @since 1.6
 */
public class Compile
    extends syntelos.sx.Response
{


    public Compile(){
        super();
    }


    public void respond(Request req)
        throws java.io.IOException,
               java.lang.InterruptedException
    {

        this.setHeader(syntelos.sx.Headers.Method.SXE);

//         if (req.hasReference()){
//             Reference source = req.getReference();
//             if (source.existsStorage()){
//                 String text_src;
//                 java.io.InputStream is = source.openInputStream();
//                 try {
//                     text_src = new alto.io.u.Bbuf(is).toString();
//                 }
//                 finally {
//                     is.close();
//                 }
//                 syntelos.uel.cx.Map cx = Configure(this,req);
//                 syntelos.uel.parser.EL el = new syntelos.uel.parser.EL(text_src);
//                 String text_dst = el.evalToString(cx);
//                 Reference target = new Reference(req.getHostName(),Target(req.getPath()));
//                 java.io.OutputStream os = target.openOutputStream();
//                 try {
//                     byte[] bits_dst = alto.io.u.Utf8.encode(text_dst);
//                     os.write(bits_dst);
//                 }
//                 finally {
//                     try {
//                         os.flush();
//                     }
//                     finally {
//                         os.close();
//                     }
//                 }
//                 target.updateMeta(req);
//                 return this;
//             }
//             else {
//                 this.setStatusNotFound();
//                 return this;
//             }
//         }

        this.setStatusNotImplemented();

    }

    private final static syntelos.uel.cx.Map Configure(Compile method, Request request){
        java.lang.ClassLoader mcl = method.getClass().getClassLoader();
        java.net.URL properties = mcl.getResource(PARSER_PROPS);
        if (null != properties){
            syntelos.uel.cx.Map re = new syntelos.uel.cx.Map(new syntelos.sys.Reference(properties));
            re.define("sx:request",request);
            re.define("rq:hostname",request.getHostName());
            re.define("rq:path",request.getPath());
            re.define("rq:port",request.getServicePort());
            re.define("rq:protocol",request.getProtocol());
            re.define("rq:type",request.getType());
            re.define("rq:pkg",request.getPkg());

            return re;
        }
        else
            throw new alto.sys.Error.Bug("Missing '"+PARSER_PROPS+"'.");
    }
    private final static String Target(String path){
        if (path.startsWith("/e/"))
            return path.substring(2);
        else
            throw new alto.sys.Error.Bug(path);
    }
    private final static String PARSER_PROPS = "syntelos/sx/e/parser.properties.xml";
}
